<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet, java.util.regex.Pattern"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String admin_email = (String) session.getAttribute("admin_email");
	if (admin_email == null) {
		//管理员没有登录或不是管理员登录
		response.sendRedirect("index.jsp");
	} else {
%>
<jsp:useBean id="bt" class="store.book.javabean.Books_Table"></jsp:useBean>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品管理 - BookStore</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
<script src="js/jquery-3.1.1.min.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="js/bootstrap.min.js"></script>

<!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
<!-- 注意： 如果通过 file://  引入 Respond.js 文件，则该文件无法起效果 -->
<!--[if lt IE 9]>
         	<script src="js/html5shiv.min.js"></script>
        	<script src="js/respond.min.js"></script>
     	<![endif]-->

<style type="text/css">

#searchBtn {
	position: absolute;
	z-index: 999;
	right: 0px;
}

#searchIn {
	padding-right: 36px;
}
</style>
</head>
<body>
	<!-- 添加书籍是否成功的结果 -->
	<%
		String handleResult = (String) session.getAttribute("HandleResult");
		if (handleResult != null) {
			if (handleResult.indexOf("成功") >= 0) {
	%>
	<div class="alert alert-success alert-dismissable">
		<button type="button" class="close" data-dismiss="alert"
			aria-hidden="true">&times;</button>
		<p><%=handleResult%></p>
	</div>
	<%
			} else {
	%>
	<div class="alert alert-danger alert-dismissable">
		<button type="button" class="close" data-dismiss="alert"
			aria-hidden="true">&times;</button>
		<p><%=handleResult%></p>
	</div>
	<%			
			}
			session.removeAttribute("HandleResult");
		}
	%>
	

	<!-- 搜索书籍 -->
	<%
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String searchBook = request.getParameter("searchIn");
		if (searchBook != null) {
			Pattern pattern = Pattern.compile("[0-9]*");
			boolean isNum = pattern.matcher(searchBook).matches();
			
			if (searchBook.trim().isEmpty()) {
	%>
	<div class="alert alert-danger alert-dismissable">
		<button type="button" class="close" data-dismiss="alert"
			aria-hidden="true">&times;</button>
		<p>请输入要搜索的书籍！</p>
	</div>
	<%
			} else if (!isNum) {
	%>
	<div class="alert alert-danger alert-dismissable">
		<button type="button" class="close" data-dismiss="alert"
			aria-hidden="true">&times;</button>
		<p>请输入正确的内容！</p>
	</div>
	<%		
			} else {
				try {
				bt.setBook_id(Long.parseLong(searchBook));
				ResultSet rs1 = bt.adminSearchBook(bt);
				if (rs1.next()) {
	%>
	<a href="#<%=rs1.getLong("book_id")%>" id='<%=rs1.getLong("book_id") + 1%>' style="display: none;"></a>
	<script type="text/javascript">
		//用js将a标签设为点击状态，以便通过锚点链接跳转到相应项
		document.getElementById('<%=rs1.getLong("book_id") + 1%>').click();
		//将跳转到的相应项高亮显示
		window.onload = function() {
			document.getElementById('<%=rs1.getLong("book_id")%>').setAttribute("style", "background: blanchedalmond;");
		};
	</script>
	<%
				} else {
	%>
	<div class="alert alert-danger alert-dismissable">
		<button type="button" class="close" data-dismiss="alert"
			aria-hidden="true">&times;</button>
		<p>没有此书！</p>
	</div>
	<%
				}
				} catch (Exception e) {}
			}
		}
	%>

	<div class="panel panel-warning">
		<div class="panel-heading">
			<span class="glyphicon glyphicon-lock"></span>管理书籍
			<div
				class="input-group col-xs-6 col-sm-4 col-md-3 col-lg-2 pull-right"
				style="margin-top: -6px;">
				<form action="administrator.jsp" method="post" id="form1">
				<input type="text" class="form-control" id="searchIn" name="searchIn"
					placeholder="搜索" /> <span class="btn glyphicon glyphicon-search"
					id="searchBtn"></span></form>
			</div>
		</div>
		<div class="panel-body table-responsive">
			<table class="table	table-striped">
				<thead>
					<tr class="text-muted">
						<th>编号</th>
						<th>书名</th>
						<th>作者</th>
						<th>单价</th>
						<th>总销量</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<%
						try {
						ResultSet rs = bt.showAllBooksInfo();
						while (rs.next()) {
					%>
						<tr id='<%=rs.getLong("book_id")%>'>
							<!--class为"modification"的为点击修改后需要修改的项-->
							<td><%=rs.getLong("book_id")%></td>
							<td><%=rs.getString("book_name")%></td>
							<td><%=rs.getString("book_author")%></td>
							<td class="modification"><%=rs.getDouble("book_price")%></td>
							<td><%=rs.getLong("book_sales_volume")%></td>
							<td><a class="text-warning" onclick="setItem(this)">修改</a><br />
							<a class="text-danger" onclick="deleteItem(this)">删除</a></td>
						</tr>
					<%
						}
						} catch (SQLException e) {}
					%>
				</tbody>
			</table>
		</div>
		<div class="panel-footer" style="height: 41px;">
			<button type="button" class="btn btn-warning pull-right"
				data-toggle="modal" data-target="#myModal" style="margin-top: -6px;">添加商品</button>
		</div>
	</div>
	
	
	<!-- 询问管理员是否删除书籍的对话框 -->
	<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">删除书籍</h4>
				</div>
				<div class="modal-body">
					确定删除吗？此操作将不可逆
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-warning" id="deleteBook" onclick="deleteBook(this)">确定</button>
				</div>
			</div>
		</div>
	</div>

	<!--添加商品模块-->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true"
		data-backdrop="static">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">添加书籍</h4>
				</div>
				<form action="servlet/AdminHandleBooks" method="post">
				<div class="modal-body">
					<label for="bookName">书名：</label><input type="text"
						class="form-control" id="bookName" name="bookName" /> <label for="author">作者：</label><input
						type="text" class="form-control" id="bookAuthor" name="bookAuthor" /> 
						<label for="bookType">类型：</label><input type="text"
						class="form-control" id="bookType" name="bookType" />
						<label
						for="price">售价：</label><input type="text" class="form-control"
						id="bookPrice" name="bookPrice" />
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="submit" class="btn btn-warning">添加</button>
				</div>
				</form>
			</div>
		</div>
	</div>

	<script>
		//删除的对话框
		function deleteItem(self) {
			$(function(){
        		$('#myModal2').modal({
        			show:true,
        			backdrop:"static",
        		});
    		});
    		
    		$('#deleteBook').val($(self).parent().parent('TR').attr("id"));
			//location.href = "servlet/AdminHandleBooks?" + "DeleteBooksId=" + $(self).parent().parent('TR').attr("id");
		}

		//删除某项
		function deleteBook(self) {
			location.href = "servlet/AdminHandleBooks?" + "DeleteBooksId=" + $(self).val();
		}

		//修改某项
		function setItem(self) {
			var value = self;
			var result = [];
			while ("TR" != self.tagName) {
				result.push(self);
				self = self.parentNode;
			}

			var len = result.length;
			var childrens = result[len - 1].parentNode.children;
			if ("修改" == value.textContent) {
				for ( var i = 0; i < childrens.length; i++) {
					if ('modification' == childrens[i].className) {
						childrens[i].innerHTML = "<input type='text' class='form-control' value='" + childrens[i].textContent + "' />";
					}
				}
				value.textContent = "保存";
			} else {
				for ( var i = 0; i < childrens.length; i++) {
					if ('modification' == childrens[i].className) {
						childrens[i].innerHTML = childrens[i].childNodes[0].value;
						value.textContent = "修改";
						location.href = "servlet/AdminHandleBooks?" + "ModifyPrice=" + childrens[i].textContent + "&ModifyId=" + $(self).attr("id");
					}
				}
			}

		}
	</script>
	
	<script type="text/javascript">
		var searchBtn = document.getElementById('searchBtn');
		searchBtn.onclick = function() {
			document.getElementById('form1').submit();
		};
	</script>
</body>
</html>
<%}%>
